<!--  -->
<template>
  <div class="Articlefl maincon">
    <!-- 搜索 -->
    <el-card shadow="hover" class="topsearch">
      <el-form :inline="true" ref="formInline" :model="formInline">
        <el-form-item label="分类名称">
          <el-input
            size="small"
            v-model="formInline.name"
            placeholder="输入分类名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-search"
            @click="search"
            >搜索</el-button
          >
          <el-button size="small" icon="el-icon-refresh" @click="resetForm"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <div class="addright">
        <el-button type="text" icon="el-icon-circle-plus" @click="handleEdit()"
          >添加分类</el-button
        >
      </div>
    </el-card>
    <!-- table -->
    <el-card shadow="hover" class="treetable">
      <el-table
        v-loading="loading"
        element-loading-text="耗尽脑汁加载中"
        :data="tableData"
        style="width: 100%"
        :header-cell-style="{ textAlign: 'center' }"
        :cell-style="{ textAlign: 'center' }"
      >
        <el-table-column type="expand" width="20px">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="所属店铺">
                <span>{{ props.row.shop }}</span>
              </el-form-item>
              <el-form-item label="商品 ID">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="店铺 ID">
                <span>{{ props.row.shopId }}</span>
              </el-form-item>
              <el-form-item label="商品分类">
                <span>{{ props.row.category }}</span>
              </el-form-item>
              <el-form-item label="店铺地址">
                <span>{{ props.row.address }}</span>
              </el-form-item>
              <el-form-item label="商品描述">
                <span>{{ props.row.desc }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="分类ID" prop="id" width="100px">
        </el-table-column>
        <el-table-column label="分类名称" prop="name"> </el-table-column>
        <el-table-column label="所属分类" prop="desc"> </el-table-column>
        <el-table-column label="备注" prop="desc"> </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="handleEdit(scope.$index, scope.row)"
              type="text"
              size="small"
              icon="el-icon-edit"
            >
              编辑
            </el-button>
            <el-button
              @click.native.prevent="deleteRow(scope.$index, tableData)"
              type="text"
              size="small"
              icon="el-icon-delete"
              style="color: red"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagecount">
        <Pagination v-bind:child-msg="pageparm" @callFather="callFather" />
      </div>
    </el-card>
    <!-- 编辑/增加 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="30%"
      @click="closeDialog"
    >
      <el-form
        label-width="120px"
        :model="editForm"
        :rules="rules"
        ref="editForm"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input
            size="small"
            v-model="editForm.name"
            auto-complete="off"
            placeholder="请输入分类名称"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item label="所属分类" prop="spell">
          <el-input
            size="small"
            v-model="editForm.spell"
            auto-complete="off"
            placeholder="请输入所属分类"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="comment">
          <el-input
            size="small"
            v-model="editForm.comment"
            auto-complete="off"
            placeholder="请输入备注"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('editForm')"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { tabData } from "./mock";
export default {
  data() {
    return {
      loading: false, //是显示加载
      // 搜索参数
      formInline: {
        name: "",
        page: 1,
        size: 10,
      },
      tableData: [],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
      // 编辑界面
      title: "添加",
      editFormVisible: false, //控制编辑页面显示与隐藏
      editForm: {
        //编辑页的数据
        name: "",
        inouttype: "",
        spell: "",
        comment: "",
        id: "",
      },
      rules: {
        name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
        spell: [{ required: true, message: "请输入所属分类", trigger: "blur" }],
      },
    };
  },

  methods: {
    // 获取列表
    getlist() {
      this.loading = true;
      this.tableData = tabData.tablelist;
      // this.pageparm.currentPage = this.formInline.page;
      // this.pageparm.pageSize = this.formInline.size;
      // this.pageparm.total = tabData.count;
      this.loading = false;
    },
    // 搜索
    search() {},
    // 重置
    resetForm() {},
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.size = parm.pageSize;
      console.log(parm);
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      console.log(row);
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.title = "修改";
        this.editForm.name = row.name;
        this.editForm.spell = row.desc;
      } else {
        this.title = "添加";
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      if (this.title == "修改") {
        this.$refs[editData].validate((valid) => {
          if (valid) {
            this.$message({
              type: "success",
              message: "保存成功！",
            });
          } else {
            return false;
          }
        });
      } else {
        this.$refs[editData].validate((valid) => {
          if (valid) {
            this.$message({
              type: "success",
              message: "保存成功！",
            });
          } else {
            return false;
          }
        });
      }
    },
    // 删除
    deleteRow(index, row) {
      console.log(index);
      console.log(row);
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "已删除!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    },
  },
  created() {
    this.getlist();
    console.log(tabData);
  },
};
</script>
<style lang='scss' scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.Articlefl {
  .topsearch {
    height: 10%;
    .el-form-item {
      margin-bottom: 0;
    }
    ::v-deep.el-card__body {
      display: flex;
      justify-content: space-between;
    }
  }
  .treetable {
    margin-top: 20px;
    height: calc(90% - 20px);
    overflow-y: auto;
    // padding-bottom: 50px;
    .pagecount {
      // width: 100%;
      // position: absolute;
      // bottom: 10px;
      // margin-top: 30px;
      // bottom: 16px;
      // background: #fff;
      // z-index: 6;
      // .page-box {
      //   margin: 10px auto;
      //   position: absolute;
      //   bottom: 10px;
      //   right: 72px;
      // }
    }
  }
}
</style>